﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;

namespace AllInOne.Pages
{
    /// <summary>
    /// ReadCardTipsPage.xaml 的交互逻辑
    /// </summary>
    public partial class ReadCardTipsPage : Page
    {


        private MainWindow parent;

        private Timer tipsTimer;

        public ReadCardTipsPage()
        {
            InitializeComponent();
        }

        public ReadCardTipsPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
            this.InitTimer();
        }


        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }

     

        public void InitPage()
        {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
        }

        private int  photoIndex = 0;
        private int interval = 2;
        private int stay = 5;

        public void InitTimer()
        {
            this.tipsTimer = new Timer();
            this.tipsTimer.Interval = 100;
            this.tipsTimer.Elapsed += new ElapsedEventHandler(delegate(object sender, ElapsedEventArgs e) {

                photoIndex++;
                int step = 1;
                if (photoIndex > 0 && photoIndex < 4 * interval)
                {
                    step = photoIndex / interval + 1;
                }
                else
                {
                    step = 5;
                    if (photoIndex > stay + 4 * interval)
                    {
                        photoIndex = 0;
                    }
                }

                this.Dispatcher.Invoke(new Action(delegate ()
                {
                    this.imgTips.Source = new BitmapImage(new Uri(string.Format("pack://application:,,,/images/put_id{0}.png",step)));
                }));


            });
            this.tipsTimer.Enabled = false;
        }

        public void StartTimer() {
            this.tipsTimer.Enabled = true;

            this.photoIndex = 0;
            Console.WriteLine("播放声音：请将您的居民二代身份证放置在感应区域。");
            this.parent.PlaySound(Properties.Resources.male_put_id, Properties.Resources.female_put_id);
        }


        public void StopTimer()
        {
            this.tipsTimer.Enabled = false;
        }
    }
}
